<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label="$t('systemManagement.projectName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input  maxlength="16" v-decorator="[ 'projectName', validatorRules.projectName]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>
        <a-form-item :label="$t('systemManagement.aliasName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input maxlength="8" v-decorator="[ 'name', validatorRules.name]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>
        <a-form-item :label="$t('base.code')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input maxlength="13" v-decorator="[ 'code', validatorRules.code]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>
        <a-form-item :label="$t('base.sysCompanyCode')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select  v-decorator="[ 'sysCompanyCode', validatorRules.sysCompanyCode]" :placeholder="$t('light.pleaseChoose')">
            <a-select-option v-for="item in dePartList"  :value="item.orgCode">
              {{item.departName}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :label="$t('base.description')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :placeholder="$t('base.pleaseEnter')" v-decorator="['remark', {}]"  />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "GlrProjectModal",
    components: { 
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        dePartList:[],
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: {rules: [
              {required: true, message: this.$t('base.pleaseEnter')+'!'}
          ]},
          projectName: {rules: [
              {required: true, message: this.$t('base.pleaseEnter')+'!'}
            ]},
          code: {rules: [
              {required: true, message: this.$t('base.pleaseEnter')+'!'}
          ]},
          remark: {rules: [
          ]},
          sysCompanyCode: {rules: [
              {required: true, message: this.$t('base.pleaseChoose')+'!'}
          ]},
        },
        url: {
          add: "/glr/glrProject/add",
          edit: "/glr/glrProject/edit",
          getDePartList: "/sys/sysDepart/getDePartList",
        }
      }
    },
    created () {
      const that = this;
      httpAction(that.url.getDePartList,null,"post").then((res)=>{
        if(res.success){
          that.dePartList=res.result;
        }else{
          //that.$message.warning(res.message);
          that.$message.warning(that.$t('base.operationFailed'));
        }
      }).finally(() => {
        that.confirmLoading = false;
      })
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'projectName','name','code','remark','sysCompanyCode'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                //that.$message.success(res.message);
                that.$message.success(that.$t('base.operationSuccessful'));
                that.$emit('ok');
              }else{
                //that.$message.warning(res.message);
                that.$message.warning(that.$t('base.operationFailed'));
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','code','remark','sysCompanyCode'))
      },

      
    }
  }
</script>